// pages/report/report.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    answerArray:[],
    rightAnswerArray:[],
    backgroundColorArray:[],
    countDownNum:"",
    percent:100,
    score:0,
    bookAbbr:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let countDownNum = options.countDownNum
    let bookAbbr = options.bookAbbr
    this.setData({
      countDownNum:countDownNum,
      bookAbbr:bookAbbr
    })

    let app = getApp()
    let answerArray = app.globalData.answerArray
    let rightAnswerArray = app.globalData.rightArray
    let questionList = app.globalData.questionList

    let backgroundColorArray = []
    let rightAnswerCounts = 0
    for(let i = 0; i < answerArray.length; ++i){
      questionList[i].practiceCounts++
      if(answerArray[i] == rightAnswerArray[i]){
        backgroundColorArray[i] = "#2ABF8E"
        questionList[i].rightAnswerCounts++
        
        rightAnswerCounts ++
      }else{
        backgroundColorArray[i] = "#FB4048"
      }
      questionList[i]["rate"] = Math.round(questionList[i].rightAnswerCounts / questionList[i].practiceCounts * 10000) / 100.00
      
    }
    let percent = (rightAnswerCounts / answerArray.length).toFixed(2) * 100
    console.log(percent)
    this.setData({
      backgroundColorArray:backgroundColorArray,
      answerArray:answerArray,
      rightAnswerArray:rightAnswerArray,
      percent:percent,
      score:rightAnswerCounts,
    })

    app.globalData.questionList = questionList

    console.log("questionList")
    console.log(questionList)

    app.globalData.userInfo.score = app.globalData.userInfo.score + rightAnswerCounts
    wx.request({
      url: app.globalData.url + '/user/updateselective',
      data: {
        score: app.globalData.userInfo.score,
        openId:app.globalData.userInfo.openid,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: result => {
        console.log(result.data)
      }
    })

  },
  goToAnalysis:function(){
    wx.navigateTo({
      url: '/pages/analysis/analysis?bookAbbr=' + this.data.bookAbbr,
    })
  },
  goToCatalog:function(){
    let app = getApp()
    let id = app.globalData.questionList[0].bookId
    wx.navigateTo({
      url: '/pages/catalog/catalog?bookId='+id+"&bookAbbr=" + this.data.bookAbbr,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})